<?php
namespace  zhtfrontend\components\services\classmanager;


use common\components\helpers\Fun;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageGoods;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\Classroom;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\GoodsClassTime;
use zhtfrontend\models\datas\GoodsTeacher;

class Createclassmanage
{
    /**
     * 创建班级
     *
     */
      public static function create($goods_id){
          $goods = Goods::findOne($goods_id);
          $attr = Fun::getGoodsAttr($goods_id);
          $attrid = Fun::getGoodsAttrId($goods_id);
          $classroom_id = $attrid['classroom_name'];
          $room = Classroom::findOne($classroom_id);
          $classroom_name = isset($room->name)?$room->name:'';
          $modelClassManage = new ClassManage();
          $modelClassManage->mechanic_id = $goods->mechanic_id;
          $modelClassManage->class_code = 'c' . time();
          $modelClassManage->code = $goods->arrangement_id;
          $modelClassManage->is_book = isset($attr['is_booking'])?$attr['is_booking']:1;
          $modelClassManage->is_old = 0;
          if ($modelClassManage->save(false)) {
              //生成班级属性
              $modelClassManageAttr = new ClassManageAttribute();
              $modelClassManageAttr->class_id = $modelClassManage->class_id;
              $modelClassManageAttr->course_id = $attrid['course'];
              $modelClassManageAttr->course = $attr['course'];
              $modelClassManageAttr->room_id = $classroom_id;
              $modelClassManageAttr->classroom_name = $classroom_name;
              $modelClassManageAttr->classroom_number = $attr['classroom_number'];
              $modelClassManageAttr->teacher_form = $attr['teacher_form'];
              $modelClassManageAttr->price = $goods->price;
              $modelClassManageAttr->class_count = 0;
              $modelClassManageAttr->suitable_age = $attr['suitable_age'];
              $modelClassManageAttr->age_group = $attr['age_group'];
              $modelClassManageAttr->class_length = $attr['class_length'];
              $modelClassManageAttr->max_number = $attr['max_number'];
              $modelClassManageAttr->opening_number = $attr['opening_number'];
              $modelClassManageAttr->is_booking = $attr['is_booking'];
              $modelClassManageAttr->region = '李沧区';
              $modelClassManageAttr->tutor_id = $goods->tutor_id;
              $modelClassManageAttr->is_group = $goods->is_group;
              $modelClassManageAttr->arrangement_id = $goods->arrangement_id;
              $modelClassManageAttr->room_type = 0;
              $modelClassManageAttr->updated_at = $goods->updated_at;
              $modelClassManageAttr->save(false);

             self::other($goods_id,$modelClassManage->class_id);
          }
      }

      public static function update($goods_id,$class_id){
          $goods = Goods::findOne($goods_id);
          $attr = Fun::getGoodsAttr($goods_id);
          $attrid = Fun::getGoodsAttrId($goods_id);
          $classroom_id = $attrid['classroom_name'];
          $room = Classroom::findOne($classroom_id);
          $classroom_name = isset($room->name)?$room->name:'';
          $modelClassManageAttr = ClassManageAttribute::find()->where(['class_id'=>$class_id])->one();
          $modelClassManageAttr->course_id = $attrid['course'];
          $modelClassManageAttr->course = $attr['course'];
          $modelClassManageAttr->room_id = $classroom_id;
          $modelClassManageAttr->classroom_name = $classroom_name;
          $modelClassManageAttr->classroom_number = $attr['classroom_number'];
          $modelClassManageAttr->teacher_form = $attr['teacher_form'];
          $modelClassManageAttr->price = $goods->price;
          $modelClassManageAttr->class_count = 0;
          $modelClassManageAttr->suitable_age = $attr['suitable_age'];
          $modelClassManageAttr->age_group = $attr['age_group'];
          $modelClassManageAttr->class_length = $attr['class_length'];
          $modelClassManageAttr->max_number = $attr['max_number'];
          $modelClassManageAttr->opening_number = $attr['opening_number'];
          $modelClassManageAttr->is_booking = $attr['is_booking'];
          $modelClassManageAttr->region = '李沧区';
          $modelClassManageAttr->tutor_id = $goods->tutor_id;
          $modelClassManageAttr->is_group = $goods->is_group;
          $modelClassManageAttr->arrangement_id = $goods->arrangement_id;
          $modelClassManageAttr->room_type = 0;
          $modelClassManageAttr->updated_at = $goods->updated_at;
          $modelClassManageAttr->save(false);
          self::other($goods_id,$class_id);
      }

      public static function other($goods_id,$class_id){
          $goodsTime = GoodsClassTime::find()->where(['goods_id'=>$goods_id])->all();
          if($goodsTime != null){
              ClassManageTime::deleteAll(['class_id'=>$class_id]);
              foreach ($goodsTime as $time){
                  $modelClassTime = new ClassManageTime();
                  $modelClassTime->class_id = $class_id;
                  $modelClassTime->class_day = $time->class_day;
                  $modelClassTime->start_time = $time->start_time;
                  $modelClassTime->end_time = $time->end_time;
                  $modelClassTime->classroom_attr_id = $time->classroom_attr_id;
                  $modelClassTime->room_id = $time->room_id;
                  $modelClassTime->save(false);
              }
          }
          $goodsTeacher = GoodsTeacher::find()->where(['goods_id'=>$goods_id])->all();
          if($goodsTeacher != null){
              ClassManageTeacher::deleteAll(['class_id'=>$class_id]);
              foreach ($goodsTeacher as $teacher){
                  $modelClassTeacher = new ClassManageTeacher();
                  $modelClassTeacher->class_id = $class_id;
                  $modelClassTeacher->teacher_id = $teacher->teacher_id;
                  $modelClassTeacher->teacher_name = $teacher->teacher_name;
                  $modelClassTeacher->save(false);
              }
          }

          $modelClassGooods = new ClassManageGoods();
          $modelClassGooods->class_id = $class_id;
          $modelClassGooods->goods_id = $goods_id;
          $modelClassGooods->created_at = time();
          $modelClassGooods->save(false);
      }
}